package com.jingyanzi.member.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.member.dao.MTagsCustomerMapper;
import com.jingyanzi.member.dao.MTagsMapper;
import com.jingyanzi.member.domain.TagDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.entity.MTags;
import com.jingyanzi.member.entity.MTagsCustomer;
import com.jingyanzi.member.entity.MTagsCustomerExample;
import com.jingyanzi.member.entity.MTagsCustomerExample.Criteria;

import com.jingyanzi.member.service.ITagService;
import com.jingyanzi.member.util.BeanUtilsExtends;

@Service("tagService")
public class TagServiceImpl extends BaseServiceImpl implements ITagService {

	@Autowired
	private MTagsMapper tagMapper;

	@Autowired
	private MTagsCustomerMapper tagsCustomerMapper;

	public ResultDO add(List<TagDO> tags, Integer customId) {
		ResultDO res = null;
		for (TagDO tagDO : tags) {
			tagDO.setCustomerId(customId);
			res = add(tagDO);
			if (!res.isSuccess()) {
				return res;
			}
		}
		return res;
	}

	public ResultDO add(TagDO domain) {

		MTags entity = new MTags();

		ResultSupport result = BeanUtilsExtends.copy(entity, domain);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "转换对象失败");
			result.setSuccess(false);
			return result;
		}

		int r = -1;
		try {
			r = tagMapper.insert(entity);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加客户标签失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加客户标签失败");
			result.setSuccess(false);
			return result;
		}

		if (!setTagMapper(entity.getId(), domain.getCustomerId())) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加客户标签失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, entity.getId());
		return result;
	}

	public boolean setTagMapper(Integer tagId, Integer customerId) {
		MTagsCustomer tagsCustomer = new MTagsCustomer();
		tagsCustomer.setCustomerId(customerId);
		tagsCustomer.setTagId(tagId);
		int r = -1;
		try {
			r = tagsCustomerMapper.insert(tagsCustomer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (r > 0) {
			return true;
		} else {
			return false;
		}
	}

	public ResultDO remove(Integer[] ids) {

		ResultSupport result = new ResultSupport();
		MTagsCustomerExample example = new MTagsCustomerExample();

		Criteria createCriteria = example.createCriteria();

		int r = -1;
		for (int i = 0; i < ids.length; i++) {
			try {
				r = tagMapper.deleteByPrimaryKey(ids[i]);
				createCriteria.andTagIdEqualTo(ids[i]);
				r = tagsCustomerMapper.deleteByExample(example);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除客户标签失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除客户标签失败");
				result.setSuccess(false);
				return result;
			}
		}

		return result;
	}

	@Override
	public ResultDO list(Integer customerId) {

		ResultSupport result = new ResultSupport();

		List<MTagsCustomer> list = null;

		MTagsCustomerExample example = new MTagsCustomerExample();

		Criteria createCriteria = example.createCriteria();

		createCriteria.andCustomerIdEqualTo(customerId);

		List<TagDO> doList = new ArrayList<TagDO>();

		try {
			list = tagsCustomerMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取客户标签失败");
			result.setSuccess(false);
			return result;
		}

		for (MTagsCustomer tc : list) {
			try {
				doList.add(getTagDO(tagMapper.selectByPrimaryKey(tc.getTagId())));
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取客户标签失败");
				result.setSuccess(false);
				return result;
			}
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, doList);

		return result;

	}

}
